package com.PVZ.view;

import com.PVZ.engine.AnimeControl;
import com.PVZ.engine.Stage;

import com.PVZ.model.Entity;
import com.PVZ.model.emuns.MoveType;
import com.PVZ.view.canvas.EntityCanvas;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * Created by @Author: kabuda on @Date: 2021/5/18 20:36
 */

@AllArgsConstructor
@NoArgsConstructor
public class EntityView extends JLabel {
    private Graphics graphics;
    private Stage stage;


    public void drawEntities(Stage stage, EntityCanvas entityCanvas) {
        this.stage = stage;
        AnimeControl animeControl = new AnimeControl();


        List<Entity> entityList = stage.getAllEntityList();
        entityList.clear();
        entityList.addAll(stage.getZombieFactory().getAllZombieList());
        entityList.addAll(stage.getPlantFactory().getAllPlantsList());
        entityList.addAll(stage.getBulletFactory().getAllBulletList());
        entityList.addAll(stage.getGettableFactory().getAllGettableList());

        entityCanvas.getImageEntities().clear();
        for (Entity entity : entityList) {
            entityCanvas.getImageEntities().add(entity);
            animeControl.animePlayer(entity);
        }
        entityMove(stage.getAllEntityList());
    }


    //控制移动
    public void entityMove(Collection<Entity> entities) {

        for (Entity entity : entities) {
            MoveType moveType = entity.getMoveType();

            //移动变量
            int x, y;
            Point point;
            if (entity.getPoint() == null) {
                return;
            }
            switch (moveType) {
                case Left:
                    x = entity.getPoint().x - entity.getCurSpeed();
                    y = entity.getPoint().y;
                    point = new Point(x, y);
                    entity.setPoint(point);
                    break;
                case LeftUp:
                    x = entity.getPoint().x - entity.getCurSpeed();
                    y = entity.getPoint().y - entity.getCurSpeed();
                    point = new Point(x, y);
                    entity.setPoint(point);
                    break;
                case Up:
                    x = entity.getPoint().x;
                    y = entity.getPoint().y - entity.getCurSpeed();
                    point = new Point(x, y);
                    entity.setPoint(point);
                    break;
                case Down:
                    x = entity.getPoint().x;
                    y = entity.getPoint().y + entity.getCurSpeed();
                    point = new Point(x, y);
                    entity.setPoint(point);
                    break;
                case Stop:
                    x = entity.getPoint().x;
                    y = entity.getPoint().y;
                    point = new Point(x, y);
                    entity.setPoint(point);
                    break;
                case Right:
                    x = entity.getPoint().x + entity.getCurSpeed();
                    y = entity.getPoint().y;
                    point = new Point(x, y);
                    entity.setPoint(point);
                    break;
                default:
                    break;
            }
        }

    }

}

